set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.13)

# set(DEBUG 1)
option(DEBUG "enable debug" 1)
set(OPT -Og)
set(BUILD_DIR build)

set(PREFIX arm-none-eabi-)
if(GCC_PATH)
  set(CMAKE_C_COMPILER ${GCC_PATH}/${PREFIX}gcc)
  set(CMAKE_CXX_COMPILER ${GCC_PATH}/${PREFIX}g++)
  set(CMAKE_ASM_COMPILER ${GCC_PATH}/${PREFIX}gcc)
  set(CMAKE_AR ${GCC_PATH}/${PREFIX}ar)
  set(CMAKE_OBJCOPY ${GCC_PATH}/${PREFIX}objcopy)
  set(CMAKE_OBJDUMP ${GCC_PATH}/${PREFIX}objdump)
  set(SIZE ${GCC_PATH}/${PREFIX}size)
else()
  set(CMAKE_C_COMPILER ${PREFIX}gcc)
  set(CMAKE_CXX_COMPILER ${PREFIX}g++)
  set(CMAKE_ASM_COMPILER ${PREFIX}gcc)
  set(CMAKE_AR ${PREFIX}ar)
  set(CMAKE_OBJCOPY ${PREFIX}objcopy)
  set(CMAKE_OBJDUMP ${PREFIX}objdump)
  set(SIZE ${PREFIX}size)
endif()

# 不加这一句会报错 undefined reference to `_exit'
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# 不加上 C CXX ASM 会报错 warning: cannot find entry symbol Reset_Handler; defaulting
# to 0000000008000000
project(demo C CXX ASM)

set(TARGET ${PROJECT_NAME}.elf)

set(C_DEFS -DUSE_HAL_DRIVER -DSTM32F105xC)
add_definitions(${C_DEFS})

set(C_INCLUDES
    Core/Inc
    USB_DEVICE/App
    USB_DEVICE/Target
    Drivers/STM32F1xx_HAL_Driver/Inc
    Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
    Middlewares/ST/STM32_USB_Device_Library/Core/Inc
    Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc
    Drivers/CMSIS/Device/ST/STM32F1xx/Include
    Drivers/CMSIS/Include)
include_directories(${C_INCLUDES})

set(C_SOURCES
    Core/Src/main.c
    Core/Src/gpio.c
    Core/Src/stm32f1xx_it.c
    Core/Src/stm32f1xx_hal_msp.c
    USB_DEVICE/App/usb_device.c
    USB_DEVICE/App/usbd_desc.c
    USB_DEVICE/Target/usbd_conf.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pcd.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pcd_ex.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_usb.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c
    Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c
    Core/Src/system_stm32f1xx.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c
    Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c)

set(ASM_SOURCES startup_stm32f105xc.s)

set(LDSCRIPT STM32F105RCTx_FLASH.ld)

set(CPU -mcpu=cortex-m3)
set(MCU ${CPU} -mthumb ${FPU} ${FLOAT-ABI})
set(LIBS -lc -lm -lnosys)
set(LIBDIR "")

set(CFLAGS ${MCU} ${C_DEFS} ${OPT} -Wall -fdata-sections -ffunction-sections)
set(CFLAGS ${CFLAGS} -MMD -MP)

if($(DEBUG) STREQUAL 1)
  set(CFLAGS ${CFLAGS} -g -gdwarf-2)
endif()

add_compile_options(${CFLAGS})

set(LDFLAGS
    ${MCU}
    -specs=nano.specs
    -T${CMAKE_SOURCE_DIR}/${LDSCRIPT}
    ${LIBDIR}
    ${LIBS}
    -Wl,-Map=${TARGET}.map,--cref
    -Wl,--gc-sections,--print-memory-usage)

add_link_options(${LDFLAGS})
add_executable(${TARGET} ${C_SOURCES} ${ASM_SOURCES})


set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(
  TARGET ${PROJECT_NAME}.elf
  POST_BUILD
  COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
  COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf>
          ${BIN_FILE}
  COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
